from botpy.types.message import Reference
from pyppeteer import launch
import os


async def getMember(api, member):  # 获取退/入频道用户基本信息
    result = await api.get_guild(guild_id=member.guild_id)
    dict1 = {"guildName": result["name"], "username": member.user.username, "userId": member.user.id}
    return dict1


async def send(api, channel_id, content):
    """
         发送主动推送消息（每日有限制）,如content数据类型为数组:
        数组只有一项时：则那一项一定为字典表示发送图片消息
         数组为两项时：第一项为文字消息，第二项为字典类型
         （
            为本地文件：{'file_image':'文件路径'}
            为网络图片：{'image':'url路径'}
         ）
         当content为str时，则为单文字消息
    """
    if not isinstance(content, list):
        await api.post_message(channel_id=channel_id, content=content)
    else:
        if len(content) == 2:
            if 'image' in content[1]:
                await api.post_message(channel_id=channel_id, content=content[0])
                await api.post_message(channel_id=channel_id, image=content[1])
            elif "file_image" in content[1]:
                await api.post_message(channel_id=channel_id, content=content[0])
                await api.post_message(channel_id=channel_id, file_image=content[1])
        else:
            if 'image' in content[0]:
                await api.post_message(channel_id=channel_id, image=content[0])
            elif "file_image" in content[0]:
                await api.post_message(channel_id=channel_id, file_image=content[0])


async def sendUser(api, guild_id, user_id, event, content):
    """
         发送主动私信消息（每日有限制）,如content数据类型为数组:
         数组只有一项时：则那一项一定为字典表示发送图片消息
         数组为两项时：第一项为文字消息，第二项为字典类型
         （
            为本地文件：{'file_image':'文件路径'}
            为网络图片：{'image':'url路径'}
         ）
         当content为str时，则为单文字消息
    """
    if not isinstance(content, list):
        if event == 1:  # 私信事件时
            await api.post_dms(guild_id=guild_id, content=content)
        elif event == 0 or event == 4:  # 频道艾特或公共消息时
            temp = await api.create_dms(guild_id=guild_id, user_id=user_id)
            await api.post_dms(guild_id=temp['guild_id'], content=content)
    else:
        if event == 1:  # 私信事件时
            if len(content) == 2:
                if 'image' in content[1]:
                    await api.post_dms(guild_id=guild_id, content=content[0])
                    await api.post_dms(guild_id=guild_id, image=content[1])
                elif "file_image" in content[1]:
                    await api.post_dms(guild_id=guild_id, content=content[0])
                    await api.post_dms(guild_id=guild_id, file_image=content[1])
            else:
                if 'image' in content[0]:
                    await api.post_dms(guild_id=guild_id, image=content[0])
                elif "file_image" in content[0]:
                    await api.post_dms(guild_id=guild_id, file_image=content[0])
        elif event == 0 or event == 4:  # 频道艾特或公共消息时
            temp = await api.create_dms(guild_id=guild_id, user_id=user_id)
            if len(content) == 2:
                if 'image' in content[1]:
                    await api.post_dms(guild_id=temp['guild_id'], content=content[0])
                    await api.post_dms(guild_id=temp['guild_id'], image=content[1])
                elif "file_image" in content[1]:
                    await api.post_dms(guild_id=temp['guild_id'], content=content[0])
                    await api.post_dms(guild_id=temp['guild_id'], file_image=content[1])
            else:
                if 'image' in content[0]:
                    await api.post_dms(guild_id=temp['guild_id'], image=content[0])
                elif "file_image" in content[0]:
                    await api.post_dms(guild_id=temp['guild_id'], file_image=content[0])


async def reply(api, msg, content, Bool=False):
    """
         发送被动消息（推荐）。Bool表示是否引用。如果content数据类型为数组:
         数组只有一项时：则那一项一定为字典表示发送图片消息
         数组为两项时：第一项为文字消息，第二项为字典类型
         （
            为本地文件：{'file_image':'文件路径'}
            为网络图片：{'image':'url路径'}
         ）
         当content为str时，则为单文字消息
    """
    if not isinstance(content, list):
        if not Bool:
            if 'DIRECT_MESSAGE_CREATE' in msg.event_id:
                await api.post_dms(guild_id=msg.guild_id, content=content, msg_id=msg.id)
            else:
                await api.post_message(channel_id=msg.channel_id, content=content, msg_id=msg.id)
        else:
            message_reference = Reference(message_id=msg.id)
            if 'DIRECT_MESSAGE_CREATE' in msg.event_id:
                await api.post_dms(guild_id=msg.guild_id, content=content, msg_id=msg.id,
                                   message_reference=message_reference)
            else:
                await api.post_message(channel_id=msg.channel_id, content=content, msg_id=msg.id,
                                       message_reference=message_reference)
    else:
        if not Bool:
            if 'DIRECT_MESSAGE_CREATE' in msg.event_id:
                if len(content) == 2:
                    if 'image' in content[1]:
                        await api.post_dms(guild_id=msg.guild_id, content=content[0], msg_id=msg.id)
                        await api.post_dms(guild_id=msg.guild_id, image=content[1]['image'], msg_id=msg.id)
                    elif "file_image" in content[1]:
                        await api.post_dms(guild_id=msg.guild_id, content=content[0], msg_id=msg.id)
                        await api.post_dms(guild_id=msg.guild_id, file_image=content[1]['file_image'], msg_id=msg.id)
                else:
                    if "image" in content[0]:
                        await api.post_dms(guild_id=msg.guild_id, image=content[0]['image'], msg_id=msg.id)
                    elif "file_image" in content[0]:
                        await api.post_dms(guild_id=msg.guild_id, file_image=content[0]['file_image'], msg_id=msg.id)
            else:
                if len(content) == 2:
                    if "image" in content[1]:
                        await api.post_message(channel_id=msg.channel_id, content=content[0], msg_id=msg.id)
                        await api.post_message(channel_id=msg.channel_id, image=content[1]['image'], msg_id=msg.id)
                    elif "file_image" in content[1]:
                        await api.post_message(channel_id=msg.channel_id, content=content[0], msg_id=msg.id)
                        await api.post_message(channel_id=msg.channel_id, file_image=content[1]['file_image'],
                                               msg_id=msg.id)
                else:
                    if "image" in content[0]:
                        await api.post_message(channel_id=msg.channel_id, image=content[0]['image'], msg_id=msg.id)
                    elif "file_image" in content[0]:
                        await api.post_message(channel_id=msg.channel_id, file_image=content[0]['file_image'],
                                               msg_id=msg.id)
        else:
            message_reference = Reference(message_id=msg.id)
            if 'DIRECT_MESSAGE_CREATE' in msg.event_id:
                if len(content) == 2:
                    if "image" in content[1]:
                        await api.post_dms(guild_id=msg.guild_id, content=content[0], msg_id=msg.id,
                                           message_reference=message_reference)
                        await api.post_dms(guild_id=msg.guild_id, image=content[1]['image'], msg_id=msg.id,
                                           message_reference=message_reference)
                    elif "file_image" in content[1]:
                        await api.post_dms(guild_id=msg.guild_id, content=content[0], msg_id=msg.id,
                                           message_reference=message_reference)
                        await api.post_dms(guild_id=msg.guild_id, file_image=content[1]['file_image'], msg_id=msg.id,
                                           message_reference=message_reference)
                else:
                    if "image" in content[0]:
                        await api.post_dms(guild_id=msg.guild_id, image=content[0]['image'], msg_id=msg.id,
                                           message_reference=message_reference)
                    elif "file_image" in content[0]:
                        await api.post_dms(guild_id=msg.guild_id, file_image=content[0]['file_image'], msg_id=msg.id,
                                           message_reference=message_reference)
            else:
                if len(content) == 2:
                    if "image" in content[1]:
                        await api.post_message(channel_id=msg.channel_id, content=content[0], msg_id=msg.id,
                                               message_reference=message_reference)
                        await api.post_message(channel_id=msg.channel_id, image=content[1]['image'], msg_id=msg.id,
                                               message_reference=message_reference)
                    elif "file_image" in content[1]:
                        await api.post_message(channel_id=msg.channel_id, content=content[0], msg_id=msg.id,
                                               message_reference=message_reference)
                        await api.post_message(channel_id=msg.channel_id, file_image=content[1]['file_image'],
                                               msg_id=msg.id,
                                               message_reference=message_reference)
                else:
                    if "image" in content[0]:
                        await api.post_message(channel_id=msg.channel_id, image=content[0]['image'], msg_id=msg.id,
                                               message_reference=message_reference)
                    elif "file_image" in content[0]:
                        await api.post_message(channel_id=msg.channel_id, file_image=content[0]['file_image'],
                                               msg_id=msg.id,
                                               message_reference=message_reference)


async def banChat(api, guild_id, user_id=False, time=60):  # 禁言函数，user_id不填默认为禁言全体。单位：秒
    if user_id:
        await api.mute_member(guild_id=guild_id, user_id=user_id, mute_seconds=time)
    else:
        await api.mute_all(guild_id=guild_id, mute_seconds=time)


async def recall(api, channel_id, msgId, hidetip=False):  # 撤回函数，hiptip的布尔值代表是否隐藏小黑条，默认为不隐藏。
    await api.recall_message(channel_id=channel_id, message_id=msgId, hidetip=hidetip)


async def screenshot(api, msg, url):
    """
    屏幕截图直接发送
    url直接填网页路径或者本地路径即可
    """
    browser = await launch()
    page = await browser.newPage()
    await page.goto(url)
    page_width = await page.evaluate('document.body.scrollWidth')
    page_height = await page.evaluate('document.body.scrollHeight')
    await page.setViewport({'width': page_width, 'height': page_height})
    await page.screenshot({'path': 'screenshot.png'})
    await browser.close()
    file_name = 'screenshot.png'
    current_dir = os.getcwd()
    absolute_path = os.path.join(current_dir, file_name)
    await reply(api, msg, [{"file_image": absolute_path}])
